#include "qdatapanel.h"

#include "../styledbar.h"
#include "../qbaseitemdelegate.h"
#include "../qactiontoolbar.h"

#include <QVBoxLayout>
#include <QHeaderView>
#include <QAction>
#include <QEvent>
#include <QSqlQuery>

QDataPanel::QDataPanel(QWidget *parent) :
    QWidget(parent),
    m_tree_widget(new QTreeWidget),
    m_page_number_label(new QLabel)
{
    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);

    m_styledbar=new StyledBar;

    StyledBar* bar=new StyledBar;

    v->addWidget(m_styledbar);
    v->addWidget(m_tree_widget);
    v->addWidget(bar);

    m_tree_widget->setItemDelegate(new QBaseItemDelegate);
    m_tree_widget->setHeaderLabels(QStringList()<<tr("No.")<<tr("Time")<<tr("Value"));
    m_tree_widget->header()->setMovable(false);
    m_tree_widget->setFrameStyle(QFrame::NoFrame);
    m_tree_widget->setRootIsDecorated(false);
    m_tree_widget->setAlternatingRowColors(true);

    setLayout(v);

    QActionToolBar *tool=new QActionToolBar;

    QAction *ac;
    QList<QAction*> list;

    ac=new QAction(this);
    ac->setText(tr("First"));
    ac->setIcon(QIcon(":/images/first.png"));
    connect(this,SIGNAL(first_enabled(bool)),ac,SLOT(setEnabled(bool)));
    connect(ac,SIGNAL(triggered()),this,SLOT(first()));
    ac->setEnabled(false);
    list.append(ac);

    ac=new QAction(this);
    ac->setText(tr("Previous"));
    ac->setIcon(QIcon(":/images/previous.png"));
    connect(this,SIGNAL(prev_enabled(bool)),ac,SLOT(setEnabled(bool)));
    connect(ac,SIGNAL(triggered()),this,SLOT(prev()));
    ac->setEnabled(false);
    list.append(ac);

    ac=new QAction(this);
    ac->setText(tr("Next"));
    ac->setIcon(QIcon(":/images/next.png"));
    connect(this,SIGNAL(next_enabled(bool)),ac,SLOT(setEnabled(bool)));
    connect(ac,SIGNAL(triggered()),this,SLOT(next()));
    ac->setEnabled(false);
    list.append(ac);

    ac=new QAction(this);
    ac->setText(tr("Last"));
    ac->setIcon(QIcon(":/images/last.png"));
    connect(this,SIGNAL(last_enabled(bool)),ac,SLOT(setEnabled(bool)));
    connect(ac,SIGNAL(triggered()),this,SLOT(last()));
    ac->setEnabled(false);
    list.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    tool->addButtonActions(list);

    QVBoxLayout *vv=new QVBoxLayout;
    vv->setMargin(0);
    vv->setSpacing(0);

    vv->addWidget(tool);
    bar->setLayout(vv);
    tool->setLabelWidget(m_page_number_label);

    m_label=new QLabel;
    m_label->setAlignment(Qt::AlignCenter);
    vv=new QVBoxLayout;
    vv->setMargin(0);
    vv->setSpacing(0);
    vv->addWidget(m_label);
    m_styledbar->setLayout(vv);

    m_label->setStyleSheet("color: rgb(255,255,255);");
    m_page_number_label->setStyleSheet("color: rgb(255,255,255);");
    m_tree_widget->setColumnWidth(0,40);
    m_tree_widget->setColumnWidth(1,130);
}

void QDataPanel::set_text(const QString &text)
{
    m_label->setText(text);
}

QString QDataPanel::text()
{
    return m_label->text();
}

void QDataPanel::set_query(QSqlQuery *query)
{
    m_query=query;
    query->first();
    m_size=0;
    while(query->next())
    {
        m_size++;
    }
    clear();
    show_page(0);

}

void QDataPanel::clear()
{
    m_page_index=0;
    m_label->setText("");
    m_tree_widget->clear();
    m_page_number_label->setText("");
    emit first_enabled(false);
    emit prev_enabled(false);
    emit next_enabled(false);
    emit last_enabled(false);
}

void QDataPanel::show_page(int index)
{
    int pt=index*PAGE_MAX;
    if(pt<0 || pt>=m_size)
    {
        return;
    }

    m_tree_widget->clear();

    m_page_index=index;

    m_query->seek(pt+1);

    int end=(pt+PAGE_MAX)>m_size?(m_size-pt):(PAGE_MAX);
    for(int i=0;i<end;i++)
    {
        QTreeWidgetItem *item=new QTreeWidgetItem(m_tree_widget);
        item->setText(0,QString::number(pt+i+1));
        item->setText(1,m_query->value(0).toString());
        item->setToolTip(1,m_query->value(0).toString());
        item->setText(2,m_query->value(1).toString());
        item->setToolTip(2,m_query->value(1).toString());
        m_query->next();
    }

    emit first_enabled(pt!=0);
    emit prev_enabled(pt!=0);
    emit next_enabled(pt+end<m_size);
    emit last_enabled(pt+end<m_size);

    m_page_number_label->setText(tr("Page:")+QString(" %1/%2").arg(m_page_index+1).arg(int((m_size-1)/PAGE_MAX)+1));
}

void QDataPanel::first()
{
    if(m_page_index!=0)
    {
        show_page(0);
    }
}

void QDataPanel::prev()
{
    if(m_page_index!=0)
    {
        show_page(m_page_index-1);
    }
}

void QDataPanel::next()
{
    if(m_page_index<((m_size-1)/PAGE_MAX))
    {
        show_page(m_page_index+1);
    }
}

void QDataPanel::last()
{
    if(m_page_index<(m_size/PAGE_MAX))
    {
        show_page((m_size-1)/PAGE_MAX);
    }
}
